package com.aispeech.task.api.runner;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
@Order(0)
public class Startup implements CommandLineRunner {
    @Autowired
    private ReactiveRedisOperations<String, String> op;

    @Value("${spring.redis.prefix.task}")
    private String taskKeyPrefix;

    @Value("${spring.redis.prefix.product}")
    private String productKeyPrefix;

    private void onerr(Throwable throwable) {
        log.error("err occurred.", throwable);
    }

    private void log(Object obj) {
        log.info("res: {}", obj);
    }

    @Override
    public void run(String... args) throws Exception {
//    	String data = "aispeech-py-t.fe9330d6d40a4fcc92a7a68c45f152c7";
//
//		int lastDotIdx = data.lastIndexOf(".");
//
//		log.info("{}, {}", data.substring(0, lastDotIdx), data.substring(lastDotIdx + 1));

//        Resource resAppVerifyLua = new ClassPathResource("lua/app_verify.lua");
//        RedisScript lua = RedisScript.of(resAppVerifyLua, List.class);
//
//        String productId = "aispeech-py-t";
//
//        op
//                .execute(lua, Arrays.asList(productId), Arrays.asList(taskKeyPrefix, productKeyPrefix))
//                .subscribe(
//                        this::log,
//                        this::onerr
//                );
    }

    private void onerr(Object o) {
        log.error("err occurred. obj: {}", o);
    }
}
